package com.yanqu.road.server.cmd.activity.rankunionwar;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

@Cmd(code = CrossProtocol.C3_CROSS_RANKUNIONWAR_UNION_EARNSPEED_RANK, desc = "")
public class Cross3RankUnionWarUnionEarnSpeedRankCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        RankUnionWarProto.CrossRankUnionWarUnionEarnSpeedRankReqMsg reqMsg = RankUnionWarProto.CrossRankUnionWarUnionEarnSpeedRankReqMsg.parseFrom(packet.getBytes());
            int activityId = reqMsg.getActivityId();
            String myUnionId = "";
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(reqMsg.getUserId());
            if(userBaseInfo != null){
                myUnionId = userBaseInfo.getUnionUid();
        }
        List<String> unionUidList = null;
        unionUidList = getTopEarnSpeedUnion(reqMsg.getEarnSpeedTopCount() > 500?reqMsg.getEarnSpeedTopCount():500, new HashSet<>(reqMsg.getServerIdList()));
        if (unionUidList == null) {
            return;
        }
        RankUnionWarProto.RankUnionWarUnionRankRespMsg.Builder respMsg = RankUnionWarProto.RankUnionWarUnionRankRespMsg.newBuilder();
        RankProto.UnionRankListMsg.Builder rankListMsg = RankProto.UnionRankListMsg.newBuilder();
        for (String s : unionUidList) {
            RankProto.UnionRankTempMsg.Builder oneMsg = RankProto.UnionRankTempMsg.newBuilder();
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(s);
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(s);
            if (unionInfo != null && unionMemberMap != null) {
                oneMsg.setUnionUid(s);
                oneMsg.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                oneMsg.setValue(unionInfo.getTotalEarnSpeed().toString());
                rankListMsg.addRankList(oneMsg);
            }
            respMsg.setRankList(rankListMsg);
            respMsg.setMyRank(getMyRank(myUnionId,unionUidList));
        }

        // 返回
        MessageHelper.sendPacket(reqMsg.getUserServerId(), reqMsg.getUserId(), YanQuMessageUtils.buildMessage(Protocol.U_RANKUNIONWAR_UNION_EARNSPEED_RANK, respMsg));
    }

    @Override
    public void sendData() {

    }


    private List<String> getTopEarnSpeedUnion(int count, Set<Long> serverIdList) {
        // 获取活动范围内全部商会
        List<CrossUnionInfo> rankList = CrossUnionMgr.getUnionListByServerList(serverIdList);
        // 排序
        rankList.sort(new Comparator<CrossUnionInfo>() {
            @Override
            public int compare(CrossUnionInfo o1, CrossUnionInfo o2) {
                return o2.getTotalEarnSpeed().compareTo(o1.getTotalEarnSpeed());
            }
        });
        // 获取前几名
        List<String> list = new ArrayList<>();
        int rank = 0;
        for (CrossUnionInfo crossUnionInfo : rankList) {
            if (rank >= count) {
                break;
            }
            rank++;
            list.add(crossUnionInfo.getUnionUid());
        }
        return list;
    }

    private int getMyRank(String myUnionId, List<String> unionIdList) {
        int rank = -1;
        if(StringUtils.isNullOrEmpty(myUnionId) || !unionIdList.contains(myUnionId)){
            return rank;
        }
        return unionIdList.indexOf(myUnionId) + 1;
    }

}
